package com.mei.demo.response;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mei.demo.enums.CommonResultCode;

/**
 * 数据返回 response 构建工具类
 * 
 * @author mei.xiaoliang@qq.com
 */
public class ResponseUtil {

    /**
     * 构建分页成功 response
     * 
     * @param list     分页 list 数据
     * @param pageInfo 分页信息（分页页码，总页数等）
     * @return
     */
    //    public static <T> String buildSuccessPageResponse(List<T> list, PageInfo<T> pageInfo) {
    //
    //        BasePageResponse<T> result = new BasePageResponse<T>();
    //        result.setResultList(list);
    //
    //        result.setPageNum(pageInfo.getPageNum());
    //        result.setPageSize(pageInfo.getPageSize());
    //        result.setTotal(pageInfo.getTotal());
    //        // bootstrap 分页不能为 0 , 不然页面报错
    //        result.setTotalPage(pageInfo.getPages() == 0 ? 1 : pageInfo.getPages());
    //
    //        result.setResult(Boolean.TRUE);
    //        result.setResultCode(CommonResultCode.SUCCESS.getCode());
    //        result.setResultString(CommonResultCode.SUCCESS.getDesc());
    //
    //        String resultString = JSON.toJSONString(result);
    //        return resultString;
    //    }

    /**
     * 
     * @param iPage
     * @return
     */
    public static <T> String buildSuccessPageResponse(IPage<T> iPage) {

        BasePageResponse<T> result = new BasePageResponse<T>();
        result.setResultList(iPage.getRecords());

        result.setPageNum((int) iPage.getCurrent());
        result.setPageSize((int) iPage.getSize());
        result.setTotal(iPage.getTotal());
        // bootstrap 分页不能为 0 , 不然页面报错
        result.setTotalPage(iPage.getPages() == 0 ? 1 : iPage.getPages());

        result.setResult(Boolean.TRUE);
        result.setResultCode(CommonResultCode.SUCCESS.getCode());
        result.setResultString(CommonResultCode.SUCCESS.getDesc());

        String resultString = JSON.toJSONString(result);
        return resultString;

    }

    /**
     * 构建成功 response（browse 查询详情）
     * 
     * @return
     */
    public static <T> String buildSuccessResponse(T t) {

        BaseResultResponse<T> result = new BaseResultResponse<T>();
        result.setResult(Boolean.TRUE);
        result.setResultCode(CommonResultCode.SUCCESS.getCode());
        result.setResultString(CommonResultCode.SUCCESS.getDesc());
        result.setResult(t);

        String resultString = JSON.toJSONString(result);
        return resultString;

    }

    /**
     * 构建成功 response（add / update / delete post 请求）
     * 
     * @return
     */
    public static String buildSuccessResponse() {

        BaseResponse result = new BaseResponse();
        result.setResult(Boolean.TRUE);
        result.setResultCode(CommonResultCode.SUCCESS.getCode());
        result.setResultString(CommonResultCode.SUCCESS.getDesc());

        String resultString = JSON.toJSONString(result);
        return resultString;

    }

    /**
     * 构建失败 response
     * 
     * @param resultCode
     * @return
     */
    public static String buildFailResponse(CommonResultCode resultCode) {

        BaseResponse result = new BaseResponse();
        result.setResult(Boolean.FALSE);
        result.setResultCode(resultCode.getCode());
        result.setResultString(resultCode.getDesc());

        String resultString = JSON.toJSONString(result);
        return resultString;

    }

}
